package com.fw.ui.view.common;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.fw.ui.bao.CaptchaBAO;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.util.UICommonUtils;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.AbstractPanel;
import com.fw.ui.view.common.event.CaptchaPanelRefreshButtonClickListener;
import com.fw.ui.view.common.util.ImageStreamSource;
import com.vaadin.server.StreamResource;
import com.vaadin.server.VaadinSession;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Image;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

@org.springframework.stereotype.Component
@Scope(SCOPE_PROTOTYPE)
public class CaptchaPanel extends AbstractPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private CaptchaBAO captchaBAO;

    @Autowired
    private CaptchaPanelRefreshButtonClickListener captchaPanelRefreshButtonClickListener;

    private VerticalLayout captchaLayout = new VerticalLayout();
    private Label lblTitle = new Label("", ContentMode.HTML);
    private Label lblTextValidateMessage = new Label("", ContentMode.HTML);

    private TextField txtCaptchaText = new TextField();
    private Button btnRefresh = new Button();
    private HorizontalLayout rowTextValidate = new HorizontalLayout();

    @Override
    public void updateStrings() {
	lblTitle.setValue(this.messageBAO.getMessage("com.fw.ui.view.common.CaptchaPanel.label.title"));
	btnRefresh.setCaption(this.messageBAO.getMessage("com.fw.ui.view.common.CaptchaPanel.button.refresh"));
    }

    @Override
    public void init() {
	this.setSizeFull();
	this.setId("captchaPanel");

	this.lblTextValidateMessage.setId("lblTextValidateMessage" + UICommonUtils.INCLUDE_VALUE_COMPONENT_PREFIX);
	this.rowTextValidate.setId("rowTextValidate" + UICommonUtils.INCLUDE_VISIBLE_COMPONENT_PREFIX);

	lblTitle.setWidth("100px");

	Label lblSep2 = new Label();
	lblSep2.setWidth("100px");

	this.lblTextValidateMessage.addStyleName("validate-error");
	txtCaptchaText.setWidth("200px");

	this.lblTextValidateMessage.setWidth("200px");

	btnRefresh.addStyleName("button-green-small");

	VerticalLayout rootlayout = new VerticalLayout();
	rootlayout.setSizeFull();

	VerticalLayout mainlayout = new VerticalLayout();
	mainlayout.setSpacing(true);
	mainlayout.setSizeUndefined();
	mainlayout.addStyleName("captcha-panel");

	VerticalLayout formLayout = new VerticalLayout();
	formLayout.setId("formCaptchaPanel");
	formLayout.setSizeFull();

	HorizontalLayout row1 = new HorizontalLayout();
	HorizontalLayout row2 = new HorizontalLayout();

	captchaLayout.setHeight("150px");
	captchaLayout.addComponent(btnRefresh);
	captchaLayout.setComponentAlignment(btnRefresh, Alignment.MIDDLE_CENTER);

	row1.setSpacing(true);
	row2.setSpacing(true);
	rowTextValidate.setSpacing(true);
	rowTextValidate.setVisible(false);

	row1.addComponent(captchaLayout);
	row1.setComponentAlignment(captchaLayout, Alignment.MIDDLE_CENTER);

	row2.addComponent(lblTitle);
	row2.addComponent(txtCaptchaText);
	row2.setComponentAlignment(lblTitle, Alignment.MIDDLE_CENTER);
	row2.setComponentAlignment(txtCaptchaText, Alignment.MIDDLE_CENTER);

	rowTextValidate.addComponent(lblSep2);
	rowTextValidate.addComponent(lblTextValidateMessage);
	rowTextValidate.setComponentAlignment(lblTextValidateMessage, Alignment.TOP_LEFT);

	formLayout.addComponent(row1);
	formLayout.addComponent(row2);
	formLayout.addComponent(rowTextValidate);

	formLayout.setComponentAlignment(row1, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row2, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(rowTextValidate, Alignment.TOP_CENTER);

	mainlayout.addComponent(formLayout);
	mainlayout.setComponentAlignment(formLayout, Alignment.MIDDLE_CENTER);

	rootlayout.addComponent(mainlayout);
	rootlayout.setComponentAlignment(mainlayout, Alignment.MIDDLE_CENTER);
	this.setContent(rootlayout);
    }

    @Override
    public void initListener() {
	captchaPanelRefreshButtonClickListener.setBasedComponent(this);
	captchaPanelRefreshButtonClickListener.setAutoEnabled(true);
	this.btnRefresh.addClickListener(captchaPanelRefreshButtonClickListener);
    }

    @Override
    public void initValidator() {

    }

    public void initData() {
	
    }

    public void reloadCaptcha() {
	try {
	    Image imgCatcha = new Image();
	    String sessionId = VaadinSession.getCurrent().getSession().getId();
	    byte[] bytes = captchaBAO.loadCaptcha(sessionId, Locale.US);
	    ImageStreamSource imageStreamSource = new ImageStreamSource();
	    imageStreamSource.setBytes(bytes);
	    StreamResource imagesource = new StreamResource(imageStreamSource, "captcha.png");
	    imgCatcha.setSource(imagesource);

	    Component oldImage = captchaLayout.getComponent(0);
	    if (oldImage != null && oldImage instanceof Image) {
		captchaLayout.removeComponent(oldImage);
	    }

	    captchaLayout.addComponent(imgCatcha, 0);
	    captchaLayout.setComponentAlignment(imgCatcha, Alignment.MIDDLE_CENTER);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    public MessageBAO getMessageBAO() {
	return messageBAO;
    }

    public Label getLblTitle() {
	return lblTitle;
    }

    public CaptchaBAO getCaptchaBAO() {
	return captchaBAO;
    }

    public Label getLblTextValidateMessage() {
	return lblTextValidateMessage;
    }

    public TextField getTxtCaptchaText() {
	return txtCaptchaText;
    }

    public HorizontalLayout getRowTextValidate() {
	return rowTextValidate;
    }

}
